package ar.com.solidussource.howmuchmobile.activities.generic;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import ar.com.solidussource.howmuchmobile.R;
import ar.com.solidussource.howmuchmobile.common.exception.ServiceException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gustavo
 * Date: 11/16/12
 * Time: 2:12 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class PaginatedGenListActivity<T> extends BaseActivity implements Runnable {
    protected List<T> data;
    protected int pageNumber = 1;
    protected int size = 10;

    private boolean rebuild = false;

    private View v;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = new ArrayList<T>();

        showProgressDialog();
        process();
    }

    protected void process(){
        new Thread(this).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            hideProgressDialog();

            Bundle bundle = msg.getData();

            if (bundle.getBoolean("rebuild")) {
                rebuild();
            }
            else {
                build();
            }

        }
    };

    @Override
    public void run(){

        try {
            serviceCall();

        } catch (ServiceException e) {
            //do something!
        }

        Message message = new Message();
        Bundle bundle = new Bundle();

        if (rebuild) {
            bundle.putBoolean("rebuild", true);
        }
        else {
            bundle.putBoolean("rebuild", false);
        }

        message.setData(bundle);

        handler.sendMessage(message);
    }

    protected void rebuild(){
        ListView lstOpciones = (ListView) v.findViewById(R.id.menuList);

        ((PaginatedGenListArrayAdapter)((HeaderViewListAdapter) lstOpciones.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();

        lstOpciones.invalidateViews();

    }

    protected void build(){
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        v = (View) inflater.inflate(R.layout.list_gen_paginated, null);

        TextView title = (TextView) v.findViewById(R.id.title);
        title.setText(getViewTitle());

        ListView lstOpciones = (ListView) v.findViewById(R.id.menuList);

        if(data.size() == size) {
            //footer paginator
            View list_gen_paginator = inflater.inflate(R.layout.list_gen_row_paginator, null);

            Button plus = (Button) list_gen_paginator.findViewById(R.id.plus);

            plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (data.size() % size == 0) {
                        pageNumber = pageNumber + 1;
                        showProgressDialog();
                        rebuild = true;
                        process();
                    }
                    else {
                        Toast toast1 = Toast.makeText(getApplicationContext(), "No hay mas datos", Toast.LENGTH_SHORT);
                        toast1.show();
                    }
                }
            });

            lstOpciones.addFooterView(list_gen_paginator);
        }

        PaginatedGenListArrayAdapter adaptador = new PaginatedGenListArrayAdapter(this);

        lstOpciones.setAdapter(adaptador);
        lstOpciones.setOnItemClickListener(new PaginatedGenListOnItemClickListener());

        setContent(v);
    }

    public abstract String getViewTitle();

    public abstract void serviceCall() throws ServiceException;

    public abstract Integer getElementId(int position);
    public abstract String getElementName(int position);


    class PaginatedGenListArrayAdapter extends BaseAdapter {
        private Activity mContext;

        PaginatedGenListArrayAdapter(Activity context) {
            mContext = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = mContext.getLayoutInflater();
            View item = inflater.inflate(R.layout.list_gen_row, null);

            TextView row = (TextView) item.findViewById(R.id.row);
            row.setText(getElementName(position));

            return item;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return getElementId(i);
        }
    }

    class PaginatedGenListOnItemClickListener implements AdapterView.OnItemClickListener {

        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            onElementClick(position);

        }

    }

    public abstract void onElementClick(int position);

}
